//
//  ViewController.m
//  跳转第三方地图
//
//  Created by apple on 2017/8/17.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ViewController.h"
#import <StoreKit/StoreKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "KCAnnotation.h"
#import "Geocoder.h"
@interface ViewController ()<SKStoreProductViewControllerDelegate, MKMapViewDelegate>


@property (nonatomic, strong) NSMutableArray *availableMaps;

@property (nonatomic, strong) MKMapView *mapView;

@property (nonatomic, strong) CLLocationManager *locationManager;



@property (nonatomic, assign) float laiFloat;
@property (nonatomic, assign) float lonFloat;

@property (nonatomic, assign) int l;

@property (nonatomic, strong) NSMutableArray *addressMutArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 50)];
    
    [btn setTitle:@"进入" forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
    self.availableMaps = [NSMutableArray arrayWithCapacity:0];
    
    [self.availableMaps addObject:@"浙江省杭州市富阳区十三号东箭集团"];
    [self.availableMaps addObject:@"浙江省杭州市余杭区"];
    [self.availableMaps addObject:@"浙江省杭州市下城区甘长东苑"];
    [self.availableMaps addObject:@"浙江省杭州市上城区陶瓷市场"];
    
    [self initGUI];
    
    //    [self createDiLiCode:@"浙江省杭州市富阳区十三号东箭集团"];
    //    [self createDiLiCode:@"浙江省杭州市富阳区品酷网咖"];
    //    [self createDiLiCode:@"浙江省杭州市下城区甘长东苑"];
    //    [self createDiLiCode:@"浙江省杭州市上城区陶瓷市场"];
    
}


#pragma mark 添加地图控件
-(void)initGUI{
    CGRect rect=[UIScreen mainScreen].bounds;
    _mapView=[[MKMapView alloc]initWithFrame:rect];
    [self.view addSubview:_mapView];
    //设置代理
    _mapView.delegate=self;
    
    //请求定位服务
    _locationManager=[[CLLocationManager alloc]init];
    if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
        [_locationManager requestWhenInUseAuthorization];
    }
    
    //用户位置追踪(用户位置追踪用于标记用户当前位置，此时会调用定位服务)
    //    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //设置地图类型
    _mapView.mapType=MKMapTypeStandard;
    
    //添加大头针
    [self addAnnotation];
}

#pragma mark 添加大头针
-(void)addAnnotation{
    
    NSMutableArray *tempMutArr = [NSMutableArray arrayWithCapacity:0];
    
    for (int l = 0; l < self.availableMaps.count; l++) {
        
        //自己写个block
        [Geocoder createDiLiCode:_availableMaps[l] success:^(NSMutableArray *mutArr) {

            //传入经纬度
            CLLocationCoordinate2D location1=CLLocationCoordinate2DMake([mutArr.firstObject floatValue],[mutArr.lastObject floatValue]);
            
            KCAnnotation *annotation1=[[KCAnnotation alloc]init];
            annotation1.title=@"CMJ Studio";
            annotation1.subtitle=@"Kenshin Cui's Studios";
            annotation1.coordinate=location1;
            [_mapView addAnnotation:annotation1];
            
            [tempMutArr addObject:annotation1];
            
            
            //此方法适应屏幕，将多个点同时存在屏幕上
            [self.mapView showAnnotations:tempMutArr animated:YES];
            
        } failed:^(NSError *error) {
            
        }];
        
        
    }
}

#pragma mark - 地图控件代理方法
#pragma mark 更新用户位置，只要用户改变则调用此方法（包括第一次定位到用户位置）
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    NSLog(@"%@",userLocation);
    //设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点)
    //        MKCoordinateSpan span=MKCoordinateSpanMake(50, 62);
    //        MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);
    //        [_mapView setRegion:region animated:true];
}

/**************我***************是****************分****************割***************线****************/



- (void)btnClick:(UIButton *)sender{
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
